﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.IO
Imports System.Net
Imports System.Text

Imports com.google.api

Imports Yahoo.API

Imports eBay.Service.Call
Imports eBay.Service.Core.Soap
Imports eBay.Service.Core.Sdk
Imports eBay.Service.Util

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim apiContext As New ApiContext

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearch.Click
        If Page.IsValid Then
            Dim _Points As New BusinessLogic.Points
            _Points.Activity = 0
            _Points.IPAddress = Request.UserHostAddress
            _Points.Comments = txtKeyword.Text
            If Session("Username") IsNot Nothing Then
                _Points.Username = Session("Username")
            Else
                _Points.Username = ""
            End If
            _Points.Save()

            Session("YahooPage") = 1

            googleVideoResult.Visible = True
            GetYahooVideoSearchWebServiceResults()
            imgBlank.Height = 30
            imgFrame.Visible = False
            pnlHyperlinkBottom.Visible = True
        End If
    End Sub
   
    Private Sub GetYahooVideoSearchWebServiceResults()
        Dim srch As String = ""
        srch = txtKeyword.Text

        Dim yahoo As New YahooSearchService

        Dim _SearchResults As New ArrayList()
        Dim Count As Integer = 10
        Dim Offset As Integer = (Integer.Parse(Session("YahooPage").ToString()) - 1) * 10 + 1

        Dim resultSet As Yahoo.API.VideoSearchResponse.ResultSet
        Dim YahooApiKey As String = ConfigurationManager.AppSettings("YahooAPIKey").ToString
        resultSet = yahoo.VideoSearch(YahooApiKey, txtKeyword.Text, "all", Count, Offset, "any", True)
        Try

            For Each result As Yahoo.API.VideoSearchResponse.ResultType In resultSet.Result
                Dim _TempSearchResult As New SearchResult()
                _TempSearchResult.Video = result.Thumbnail.Url
                _TempSearchResult.Url = result.Url
                _TempSearchResult.Title = result.Title
                If result.Title.Length > 20 Then
                    _TempSearchResult.Text = result.Title.Substring(0, 17) & "..."
                Else
                    _TempSearchResult.Text = result.Title
                End If

                _TempSearchResult.Duration = result.Duration
                _TempSearchResult.Streaming = result.Streaming
                _TempSearchResult.Cached = result.FileSize
                _SearchResults.Add(_TempSearchResult)

            Next

        Catch ex As Exception

        End Try
        litYahooSearchCount.Text = String.Format("Page {0} of {1}", Session("YahooPage"), CInt(resultSet.totalResultsAvailable / 10))
        If Integer.Parse(Session("YahooPage").ToString()) = 1 Then
            lnkYahooPrev.Visible = False
        Else
            lnkYahooPrev.Visible = True
        End If
        If Integer.Parse(Session("YahooPage").ToString()) = CInt(resultSet.totalResultsAvailable / 10) Then
            lnkYahooNext.Visible = False
        Else
            lnkYahooNext.Visible = True
        End If

        yahooVideoRes.DataSource = _SearchResults
        yahooVideoRes.DataBind()
    End Sub

    Protected Sub lnkYahooPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkYahooPrev.Click
        Session("YahooPage") = Integer.Parse(Session("YahooPage").ToString()) - 1
        GetYahooVideoSearchWebServiceResults()
    End Sub

    Protected Sub lnkYahooNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkYahooNext.Click
        Session("YahooPage") = Integer.Parse(Session("YahooPage").ToString()) + 1
        GetYahooVideoSearchWebServiceResults()
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("SearchText") IsNot Nothing Then
                txtKeyword.Text = Session("SearchText").ToString()
                Session("YahooPage") = 1
                googleVideoResult.Visible = True
                GetYahooVideoSearchWebServiceResults()
                imgBlank.Height = 30
                imgFrame.Visible = False
                pnlHyperlinkBottom.Visible = True
            End If
        Else
            If txtKeyword.Text.Length > 0 Then
                Session("SearchText") = txtKeyword.Text
            Else
                Session("SearchText") = Nothing
            End If
        End If
    End Sub
    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = True
        If txtKeyword.Text.Length = 0 Then
            args.IsValid = False
        End If
    End Sub

    Public Class SearchResult
        Private _Video As String
        Property Video() As String
            Get
                Return _Video
            End Get
            Set(ByVal value As String)
                _Video = value
            End Set
        End Property
        Private _Url As String
        Property Url() As String
            Get
                Return _Url
            End Get
            Set(ByVal value As String)
                _Url = value
            End Set
        End Property

        Private _Title As String
        Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property

        Private _Text As String
        Property Text() As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
        Private _Duration As String
        Property Duration() As String
            Get
                Return _Duration
            End Get
            Set(ByVal value As String)
                _Duration = value
            End Set
        End Property

        Private _Streaming As String
        Property Streaming() As String
            Get
                Return _Streaming
            End Get
            Set(ByVal value As String)
                _Streaming = value
            End Set
        End Property
        Private _Cached As String
        Property Cached() As String
            Get
                Return _Cached
            End Get
            Set(ByVal value As String)
                _Cached = value
            End Set
        End Property
    End Class
End Class